package site.itaoke.config;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import java.util.UUID;

/**
 * Created by sunfeihu on 2017/10/19.
 */
public class IndexController extends Controller {


    public void create() {
        render("/create.ftl");
    }

    public void doCreate() {
        String url = this.getPara("url");
        Record record = Db.findFirst(" select * from urls where url = ? limit 0,1 ", new Object[]{
                url
        });
        if (null != record) {
            renderText(record.getStr("uuid"));
            return;
        }
        String uuid = UUID.randomUUID().toString();
        uuid = uuid.replaceAll("-", "");
        Db.update(" insert into urls (uuid,url) values(?,?) ", new Object[]{uuid, url});
        renderText(uuid);
    }

    public void jump() {
        String uuid = this.getPara("uuid");
        Record record = Db.findFirst("select * from urls where uuid = ? limit 0,1 ", new Object[]{
                uuid
        });
        if (null == record) {
            renderText("fail");
            return;
        }
        this.setAttr("url", record.getStr("url"));
        render("/jump.ftl");
    }

}
